/**
* Created by alan on 17-5-1.
*/
<template>
  <section>
    <el-dialog title="修改密码" :visible.sync="dialogVisible" size="tiny">
      <el-form :model="inputForm" label-position="left" label-width="100px" :rules="inputRule" ref='inputForm'>
        <el-form-item label="原密码" prop="oldPwd">
          <el-input type="password" v-model="inputForm.oldPwd" auto-complete="off" :maxlength="16"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="newPwd">
          <el-input type="password" v-model="inputForm.newPwd" auto-complete="off" :maxlength="16"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="newPwd2">
          <el-input type="password" v-model="inputForm.newPwd2" auto-complete="off" :maxlength="16"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit('inputForm')" :loading="inputFormLoading">确 定</el-button>
      </div>
    </el-dialog>
  </section>
</template>
<script>
export default {
  data() {
    let validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入新密码"));
      } else {
        callback();
      }
    };
    
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.inputForm.newPwd) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      dialogVisible: false,
      inputFormLoading: false,
      inputForm: {
        oldPwd: "",
        newPwd: "",
        newPwd2: ""
      },
      inputRule: {
        oldPwd: [
          {
            required: true,
            message: "请输入原密码",
            trigger: "blur"
          }
        ],
        newPwd: [
          {
            validator: validatePass,
            required: true,
            trigger: "blur"
          }
        ],
        newPwd2: [
          {
            validator: validatePass2,
            required: true,
            trigger: "blur"
          }
        ]
      }
    };
  },

  // props: ['changeType', 'username','uuid'],
  methods: {
    show() {
      this.dialogVisible = true;
    },

    hide() {
      this.dialogVisible = false;
    },
    submit(formName) {
     
      // console.log(_data);
      this.$refs[formName].validate(valid => {
        console.log(valid);
        if (valid) {
         let _data = {
        oldPassword: this.inputForm.oldPwd,
        password: this.inputForm.newPwd
      };
          console.log(_data);
          // if (1 == this.changeType) {
          //   _path = "auth/forceChangePassword.json"
          //   _data.username = this.username
          // } else {
          //   _data.uuid = this.uuid;
          //   _path = "manage/sysUserPass"
          // }
          this.inputFormLoading = true;

          this.$http.post("/shop/editPwd", _data).then(
            res => {
              this.inputFormLoading = false;
              console.log(res);
               this.dialogVisible = false
              if (res.data.status == 1) {
                this.$refs.inputForm.resetFields()
               
                this.$message({
                  message: '密码修改成功,请重新登录',
                  type: 'success',
                  onClose: () => {
                    this.$router.replace('/login')
                  }
                })
              } else {
                this.$message.error(res.data.message);
              }
            },
            res => {
              this.inputFormLoading = false;
            }
          );
        } else {
          return false;
        }
      });
    }
  },
  mounted: function() {}
};
</script>
<style scoped>
</style>
